package com.github.catvod.spider.merge;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/* loaded from: classes.dex */
public class qd extends ImageView {
    public boolean V0;
    private String h;
    private Handler j;

    class Tw extends Handler {
        Tw() {
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            int i = message.what;
            if (i == 1) {
                qd.this.setImageBitmap((Bitmap) message.obj);
            } else if (i == 2) {
                Toast.makeText(qd.this.getContext(), "网络连接失败", 0).show();
            } else {
                if (i != 3) {
                    return;
                }
                Toast.makeText(qd.this.getContext(), "服务器发生错误", 0).show();
            }
        }
    }

    class yh extends Thread {
        yh() {
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            Bitmap q;
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(qd.this.h).openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setConnectTimeout(10000);
                if (httpURLConnection.getResponseCode() != 200) {
                    qd.this.j.sendEmptyMessage(3);
                    return;
                }
                InputStream inputStream = httpURLConnection.getInputStream();
                qd qdVar = qd.this;
                if (qdVar.V0) {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    try {
                        byte[] bArr = new byte[1024];
                        while (true) {
                            int read = inputStream.read(bArr);
                            if (read <= -1) {
                                break;
                            } else {
                                byteArrayOutputStream.write(bArr, 0, read);
                            }
                        }
                        byteArrayOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                    ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                    q = qd.this.q(byteArrayInputStream);
                    qd.this.j(byteArrayInputStream2);
                } else {
                    q = qdVar.q(inputStream);
                }
                Message obtain = Message.obtain();
                obtain.obj = q;
                obtain.what = 1;
                qd.this.j.sendMessage(obtain);
                inputStream.close();
            } catch (IOException e2) {
                e2.printStackTrace();
                qd.this.j.sendEmptyMessage(2);
            }
        }
    }

    public qd(Context context) {
        super(context);
        this.V0 = false;
        this.j = new Tw();
    }

    public int BQ() {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        int height = getHeight();
        if (height <= 0) {
            height = layoutParams.height;
        }
        if (height <= 0) {
            height = getMaxHeight();
        }
        if (height <= 0) {
            height = displayMetrics.heightPixels;
        }
        Log.e("ImageView实际的高度", String.valueOf(height));
        return height;
    }

    public int I(BitmapFactory.Options options) {
        int round;
        int W = W();
        int BQ = BQ();
        int i = options.outWidth;
        Log.e("网络图片实际的宽度", String.valueOf(i));
        int i2 = options.outHeight;
        Log.e("网络图片实际的高度", String.valueOf(i2));
        if (i > W || i2 > BQ) {
            int round2 = Math.round(i / W);
            round = Math.round(i2 / BQ);
            if (round2 > round) {
                round = round2;
            }
        } else {
            round = 1;
        }
        Log.e("压缩比率", String.valueOf(round));
        return round;
    }

    public void Qy() {
        File file = new File(getContext().getCacheDir(), oC());
        if (file.length() <= 0) {
            oq();
            Log.e("MyImageView", "使用网络图片");
            return;
        }
        try {
            Bitmap q = q(new FileInputStream(file));
            Message obtain = Message.obtain();
            obtain.obj = q;
            obtain.what = 1;
            this.j.sendMessage(obtain);
            Log.e("MyImageView", "使用缓存图片");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public int W() {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        int width = getWidth();
        if (width <= 0) {
            width = layoutParams.width;
        }
        if (width <= 0) {
            width = getMaxWidth();
        }
        if (width <= 0) {
            width = displayMetrics.widthPixels;
        }
        Log.e("ImageView实际的宽度", String.valueOf(width));
        return width;
    }

    public void j(InputStream inputStream) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(getContext().getCacheDir(), oC()));
            byte[] bArr = new byte[1024];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    fileOutputStream.close();
                    Log.e("MyImageView", "缓存成功");
                    return;
                }
                fileOutputStream.write(bArr, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("MyImageView", "缓存失败");
        }
    }

    public String oC() {
        StringBuilder sb = new StringBuilder();
        for (String str : this.h.split("\\/")) {
            sb.append(str);
        }
        Log.e("MyImageView", "文件名：" + sb.toString());
        return sb.toString();
    }

    public void oq() {
        new yh().start();
    }

    public Bitmap q(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            byte[] bArr = new byte[1024];
            while (true) {
                int read = inputStream.read(bArr);
                if (read <= -1) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
            byteArrayOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(byteArrayInputStream, null, options);
        options.inSampleSize = I(options);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(byteArrayInputStream2, null, options);
    }

    public void u(String str) {
        this.h = str;
        if (this.V0) {
            Qy();
        } else {
            oq();
        }
    }
}
